import web from '@ohos.web.webview'
import { KvManager } from './KvManager'
import Logger from './Logger'

export interface MethodData {
  title?: string,
  url?: string
  nickName?: string
  avatar?: string
  hint ?: string
  keyWord?: string
  successToUrl ?: string
  imgUrl?: string
  searchKeyWord?: string
  type?: number
  showShareBtn?: number
  showRefresh?: number
  showComplaint?: number
  selectLimit?: number
  tag?: number
  isRefresh ?: boolean
  isDeviceHead  ?: boolean
  isCloseBeforePage ?: boolean
  takePhotoOnly?: boolean
  isBackSet?: boolean
}

export class WebUtil {
  public controller: web.WebviewController
  public callback: (tag: string, d: MethodData) => void
  public methodList: Array<string> =
    ["gotoPrivacy",
      "hideNavigation",
      "getAdBehavior",
      "navigation",
      "gotofeedback",
      "imgShow",
      "downloadSignPhoto",
      "openNewWeb",
      "showNoBack",
      "showAppLogin",
      "setTitle",
      "login",
      "cancelBack",
      "shareClick",
      "close",
      "getPhotos",
      "getPhoto",
      "makeCall",
      "getGPS",
      "scanningQrcode",
      "showSearchBar",
      "showTitleBar",
      "showMenuBtn",
      "setNewTokenToApp",
      "setVerification",
      "openDefaultExplorer",
      "setAccessToken",
      "getAuthentication",
      "setAuthentication",
      "showBarRightBtn",
      "getUserClickBehavior",
      "getAccessToken",
      "copyText",
      "refreshData",
      "getDeviceMessage",
      "callDeviceLiveBroadcast",
      "showDeviceHead",
      "userBasicInfoChange",
      "showDownload"]
  public methodObj
  public context: any

  constructor(context: any, callback: (tag: string, d: MethodData) => void) {
    this.context = context
    this.callback = callback
    this.controller = new web.WebviewController()
    this.methodObj = {
      userBasicInfoChange: this.userBasicInfoChange,
      showDeviceHead: this.showDeviceHead,
      callDeviceLiveBroadcast: this.callDeviceLiveBroadcast,
      getDeviceMessage: this.getDeviceMessage,
      refreshData: this.refreshData,
      copyText: this.copyText,
      getAccessToken: this.getAccessToken,
      getUserClickBehavior: this.getUserClickBehavior,
      showBarRightBtn: this.showBarRightBtn,
      setAuthentication: this.setAuthentication,
      getAuthentication: this.getAuthentication,
      setAccessToken: this.setAccessToken,
      openDefaultExplorer: this.openDefaultExplorer,
      setVerification: this.setVerification,
      setNewTokenToApp: this.setNewTokenToApp,
      showMenuBtn: this.showMenuBtn,
      showTitleBar: this.showTitleBar,
      showSearchBar: this.showSearchBar,
      getKeyboardHeight: this.getKeyboardHeight,
      scanningQrcode: this.scanningQrcode,
      setNoSearch: this.setNoSearch,
      getGPS: this.getGPS,
      makeCall: this.makeCall,
      getPhoto: this.setPhoto,
      getPhotos: this.setPhoto,
      shareClick: this.shareClick,
      showNoBack: this.showNoBack,
      showAppLogin: this.showAppLogin,
      setTitle: this.setTitle,
      imgShow: this.imgShow,
      downloadSignPhoto: this.downloadSignPhoto,
      navigation: this.navigation,
      getAdBehavior: this.getAdBehavior,
      openNewWeb: this.openNewWeb,
      close: this.close,
      login: this.login,
      gotofeedback: this.gotofeedback,
      showDownload: this.showDownload,
      hideNavigation: this.hideNavigation,
      gotoPrivacy: this.gotoPrivacy
    }
  }

  getAccessToken = () => {
    const methodData = this.method('getAccessToken')

    this.onGetAccessToken()
  }
  showDeviceHead = (data) => {
    const methodData = this.method('showDeviceHead', data)
  }

  callDeviceLiveBroadcast = (data) => {
    const methodData = this.method('callDeviceLiveBroadcast', data)
  }

  getDeviceMessage = (data) => {
    const methodData = this.method('getDeviceMessage', data)
  }

  refreshData = (data) => {
    const methodData = this.method('refreshData', data)
  }

  copyText = (data) => {
    const methodData = this.method('copyText', data)
  }

  getUserClickBehavior = (data) => {
    const methodData = this.method('getUserClickBehavior', data)
  }

  showBarRightBtn = (data) => {
    const methodData = this.method('showBarRightBtn', data)
  }

  getAuthentication = (data) => {
    const methodData = this.method('getAuthentication', data)
  }

  setAuthentication = (data) => {
    const methodData = this.method('setAuthentication', data)
  }

  setAccessToken = (data) => {
    const methodData = this.method('setAccessToken', data)
  }

  openDefaultExplorer = (data) => {
    const methodData = this.method('openDefaultExplorer', data)
  }

  setVerification = (data) => {
    const methodData = this.method('setVerification', data)
  }

  setNewTokenToApp = (data) => {
    const methodData = this.method('setNewTokenToApp', data)
  }

  showMenuBtn = (data) => {
    const methodData = this.method('showMenuBtn', data)
  }

  showTitleBar = (data) => {
    const methodData = this.method('showTitleBar', data)
  }

  showSearchBar = (data) => {
    const methodData = this.method('showSearchBar', data)

  }

  getKeyboardHeight = (data) => {
    const methodData = this.method('getKeyboardHeight', data)
  }

  scanningQrcode = (data) => {
    const methodData = this.method('scanningQrcode', data)
  }

  setNoSearch = (data) => {
    const methodData = this.method('setNoSearch', data)
  }

  getGPS = (data) => {
    const methodData = this.method('getGPS', data)
  }

  makeCall = (data) => {
    const methodData = this.method('makeCall', data)
  }

  shareClick = (data) => {
    const methodData = this.method('shareClick', data)
  }

  showNoBack = (data) => {
    const methodData = this.method('showNoBack', data)
  }

  showAppLogin = (data) => {
    const methodData = this.method('showAppLogin', data)
  }

  setTitle = (data) => {
    const methodData = this.method('setTitle', data)
    this.callback('setTitle', methodData)
  }

  imgShow = (data) => {
    const methodData = this.method('imgShow', data)
  }

  downloadSignPhoto = (data) => {
    const methodData = this.method('downloadSignPhoto', data)
  }

  navigation = (data) => {
    const methodData = this.method('navigation', data)
  }

  getAdBehavior = (data) => {
    const methodData = this.method('getAdBehavior', data)
  }

  openNewWeb = (data) => {
    const methodData = this.method('openNewWeb', data)
  }

  close = () => {
    const methodData = this.method('close')
  }

  login = () => {
    const methodData = this.method('login')
  }

  gotofeedback = () => {
    const methodData = this.method('gotofeedback')
  }

  hideNavigation = () => {
    const methodData = this.method('hideNavigation')
  }

  showDownload = () => {
    const methodData = this.method('showDownload')
  }

  gotoPrivacy = () => {
    const methodData = this.method('gotoPrivacy')
  }

  userBasicInfoChange = (data) => {
    const methodData = this.method('userBasicInfoChange', data)
  }

  method(name: string, data?: string): MethodData {
    Logger.info("androidjs", 'name==' + name)
    Logger.info("androidjs", 'data==' + data)
    if (data) {
      const methodData = JSON.parse(data)
      return methodData
    }

  }

  executeJS(name: string) {
    Logger.info("androidjs", 'executeJS name' + "===" + name)
    this.controller.runJavaScript(name, (error, result) => {
      Logger.info("androidjs", 'name=' + name)
      Logger.info("androidjs", "====result=" + result)
      Logger.info("androidjs", "====error=" + error)
    })

  }

  onKeyboardUp(data: string) {
    const jsMethodName = 'onKeyboardUp( 1,' + "成功" + ',' + data + ' )'
    this.executeJS(jsMethodName)
  }

  onDeviceObtained(data: string) {
    const jsMethodName = 'onDeviceObtained( 1,' + "成功" + ',' + data + ' )'
    this.executeJS(jsMethodName)
  }

  onQrCodeScanned(data: string) {
    const jsMethodName = 'onQrCodeScanned( 1,' + "成功" + ',' + data + ' )'
    this.executeJS(jsMethodName)
  }

  onGPSLocated(data: string) {
    const jsMethodName = 'onGPSLocated( 1,' + "成功" + ',' + data + ' )'
    this.executeJS(jsMethodName)
  }

  setPhoto(base64Image: string) {
    const jsMethodName = "setPhoto('" + base64Image + "')"
    this.executeJS(jsMethodName)
  }

  async onGetAccessToken() {
    var kvManager: KvManager = new KvManager(this.context)
    await kvManager.initKvManager()
    const token = await kvManager.getWebTolen()
    const jsMethodName = 'GetAccessToken("' + token + '")'
    this.executeJS(jsMethodName)
  }

  OnAuthentication(data: string) {
    const jsMethodName = "OnAuthentication('" + data + "')"
    this.executeJS(jsMethodName)
  }

  OnsetAuthentication(data: string) {
    const jsMethodName = "OnsetAuthentication('" + data + "')"
    this.executeJS(jsMethodName)
  }

  onKeywords(data: string) {
    const jsMethodName = "javascript:AppCallback.onKeywords('" + data + "')"
    this.executeJS(jsMethodName)
  }

  onLoadToken(data: string) {
    const jsMethodName = "onLoadToken('" + data + "')"
    this.executeJS(jsMethodName)
  }

  setWebToken(data: string) {
    const jsMethodName = "setWebToken('" + data + "')"
    this.executeJS(jsMethodName)
  }

  refreshBackData() {
    const jsMethodName = "refreshBackData()"
    this.executeJS(jsMethodName)
  }


  barLeftDidClick() {
    const jsMethodName = "barLeftDidClick()"
    this.executeJS(jsMethodName)
  }

  refreshPage() {
    const jsMethodName = "refreshPage()"
    this.executeJS(jsMethodName)
  }
}